﻿using Dycox.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dycox.Reporting
{
    public class DataSheetCellSettings
    {
        public DataSheetCellSettings()
        {
            
        }

        private string _text;
        [XmlAttribute("text")]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private ElementStyle _style;
        [XmlElement("style")]
        public ElementStyle Style
        {
            get { return _style; }
            set { _style = value; }
        }

        private DataSheetColumn _column;

        public DataSheetColumn Column
        {
            get { return _column; }
            internal set { _column = value; }
        }

    }

    public class DataSheetHeaderCell : DataSheetCellSettings
    {
        private string _group;
        [XmlAttribute("group")]
        public string Group
        {
            get { return _group; }
            set { _group = value; }
        }

    }

    public class DataSheetBodyCell : DataSheetCellSettings
    {
        public DataSheetBodyCell()
        {

        }

        private string _dataMember;
        [XmlAttribute("dataMember")]
        public string DataMember
        {
            get { return _dataMember; }
            set { _dataMember = value; }
        }

        private string _format;
        [XmlAttribute("format")]
        public string Format
        {
            get { return _format; }
            set { _format = value; }
        }

        private string _nullText;
        [XmlAttribute("nullText")]
        public string NullText
        {
            get { return _nullText; }
            set { _nullText = value; }
        }


        internal string GetText(object dataItem, int itemIndex)
        {
            string text;

            if (!string.IsNullOrEmpty(_dataMember))
            {
                if (dataItem != null)
                    text = Utils.DataBinderUtil.Eval(dataItem, _dataMember, _format, _nullText);
                else
                    text = string.Empty;
            }
            else if (!string.IsNullOrEmpty(Text))
            {
                if (Text == "#")
                {
                    //text = (itemIndex + 1).ToString();
                    int rownum = itemIndex + 1;

                    if (!string.IsNullOrEmpty(_format))
                        text = string.Format(_format, rownum);
                    else
                        text = rownum.ToString();
                }
                else
                {
                    if (dataItem != null)
                        text = Utils.DataBinderUtil.EvalTemplate(Text, dataItem);
                    else
                        text = string.Empty;
                }
            }
            else
                text = string.Empty;

            return text ?? _nullText ?? string.Empty;
        }
    }

    public class DataSheetFooterCell : DataSheetBodyCell
    {
        public DataSheetFooterCell()
        {

        }

        private AggregationFunction _aggregation;
        [XmlAttribute("aggregation")]
        public AggregationFunction Aggregation
        {
            get { return _aggregation; }
            set { _aggregation = value; }
        }


    }
}
